package io.fatmore.web.api;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * <b><code>ApiResponse</code></b>
 * <p>
 * 
 * </p>
 * <b>Creation Time:</b> 2016年9月12日 下午9:57:27
 * @author shangkun.lsk@fastmor.com
 * @since ucun-finance
 */
public class ApiResponse implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5386040809578526766L;

	private String version = "1.0.0";
	/**
	 * 状态号
	 */
	private int code = 2000;
	private String message = "success";
	private Object data = null;
	private Map<String, Object> values = new HashMap<String, Object>();//附加的属性
	private String callback = "";//对于前端需要同时异步发起的请求，应保留callback机制

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public int getCode() {
		return code;
	}

	public ApiResponse setCode(int code) {
		this.code = code;
		return this;
	}

	public String getCallback() {
		return callback;
	}

	public ApiResponse setCallback(String callback) {
		this.callback = callback;
		return this;
	}

	public String getMessage() {
		return message;
	}

	public ApiResponse setMessage(String message) {
		this.message = message;
		return this;
	}
	
	public ApiResponse tips(String message){
		return this.setMessage(message);
	}

	//for json
	public Object getData() {
		if(this.data == null){
			return null;
		}
		//擢升一层
		return this.data;
	}

	public ApiResponse setResponseData(Object data) {
		this.data = data;
		return this;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Map<String, Object> getValues() {
		return values;
	}

	public ApiResponse putValue(String key, Object value) {
		this.values.put(key, value);
		return this;
	}

	public boolean isError() { return this.code != 2000; }

	//------------------------------------------------------------------------------------
	public static ApiResponse notLogin(String message) {
		ApiResponse response = new ApiResponse() ;
		response.setCode(ApiResponseCode.not_login) ;
		if(message == null) {
			message = "please login first." ;
		}
		response.setMessage(message) ;
		return response ;
	}
	/**
	 * 返回错误数据体
	 * @param message
	 * @return
	 */
	public static ApiResponse error(String message) {
		ApiResponse response = new ApiResponse();
		response.setCode(ApiResponseCode.error);
		if(message == null){
			message = "error";
		}
		response.setMessage(message);
		return response;
	}
	
	public ApiResponse error(){
		return error(null);
	}
	
	public static ApiResponse nothing(){
		//减少服务器带宽
		return null;
	}
	
	public static ApiResponse illegal(){
		return illegal(null);
	}
	public static ApiResponse illegal(String message){
		ApiResponse response = new ApiResponse();
		response.setCode(ApiResponseCode.illegal);
		if(message == null){
			message = "parameter illegal";
		}
		response.setMessage(message);
		return response;
	}
	
	public static ApiResponse authenticateFail(String message){
		ApiResponse response = new ApiResponse();
		response.setCode(ApiResponseCode.authenticate_fail);
		if(message == null){
			message = "authenticate fail";
		}
		response.setMessage(message);
		return response;
	}
	
	public static ApiResponse needLogin(String message){
		if(message == null){
			message="need to login.";
		}
		ApiResponse response = new ApiResponse();
		response.setCode(ApiResponseCode.not_login);
		response.setMessage(message);
		return response;
	}
	
	/**
	 * 成功的返回数据体
	 * @param data
	 */
	public static ApiResponse success(String message, Object data){
		ApiResponse response = new ApiResponse();
		response.setCode (ApiResponseCode.ok);
		response.setData (data);
		response.setMessage( message);
		return response;
		
	}
	
	public static ApiResponse success(){
		return success(null);
	}
	
	public static ApiResponse successNoData(String message){
		ApiResponse response = new ApiResponse();
		response.setCode(ApiResponseCode.ok);
		response.setMessage(message);
		return response;
	}

	public static ApiResponse successNoData(){
		ApiResponse response = new ApiResponse();
		response.setCode(ApiResponseCode.ok);
		response.setMessage("succ");
		return response;
	}
	
	public static ApiResponse success(Object data){
		ApiResponse response = new ApiResponse();
		response.setCode(ApiResponseCode.ok);
		response.setMessage("success");
		response.setData(data);
		return response;
	}
	
	public static ApiResponse redirection(String message){
		ApiResponse response = new ApiResponse();
		response.setCode(ApiResponseCode.redirection);
		response.setMessage(message);
		return response;
	}
}
